//  This file is part of Swordbible - http://www.swordwarrior.net
//  Copyright (C) 2003-2009 Manu Mital mital.manu@gmail.com

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.



// SwordBible.cpp : Defines the class behaviors for the application.
//

#include <stdafx.h>
#include "SwordBible.h"
#include "MainFrm.h"
#include "Devotional.h"
#include "SwordBibleDoc.h"
#include "SwordBibleView.h"
#include "afxwin.h"
#include "Splasher.h"
#include ".\SwordBible.h"
#include "TransparentImage.h"


#ifndef SPI_SETDROPSHADOW
#define SPI_SETDROPSHADOW                   0x1025
#endif

//Our simple derived Splash screen class, which just uses the bitmap resource IDB_SPLASH

class CMySplashWnd : public CSplashWnd
{
public:
  CMySplashWnd();
  DECLARE_DYNCREATE(CMySplashWnd);
};

IMPLEMENT_DYNCREATE(CMySplashWnd, CSplashWnd);

CMySplashWnd::CMySplashWnd()
{ 

  SetBitmapToUse(IDB_SPLASH);
  SetDropShadow(); 
}


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSwordBibleApp

BEGIN_MESSAGE_MAP(CSwordBibleApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
END_MESSAGE_MAP()



// CSwordBibleApp construction

CSwordBibleApp::CSwordBibleApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance

}


// The one and only CSwordBibleApp object

CSwordBibleApp theApp;

// CSwordBibleApp initialization

BOOL CSwordBibleApp::InitInstance()
{
//TODO: call AfxInitRichEdit2() to initialize richedit2 library.
	CSplashFactory factory;
	if (!factory.Create(RUNTIME_CLASS(CMySplashWnd)))
	{
    AfxMessageBox(_T("Failed to create splash screen"), MB_OK | MB_ICONSTOP);
    return FALSE;
	}



	// InitCommonControls() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	InitCommonControls();

	CWinApp::InitInstance();

	UpdatePrinterSelection(TRUE);

	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}
	AfxEnableControlContainer();

	
	//create the .ini file in application's folder
	CString path = _T("");
	CBibleSheet::GetAppDirectory(path);
  CSwordBibleDoc::driveLetter = path.GetAt(0);

#if defined VERSION_PORTABLE
  path.Replace(_T("\\App\\SwordBible"), _T("\\Data\\settings\\SwordBible.ini"));

  if(GetFileAttributes(path) == -1)
  {
    CString dir = path;
    dir.Replace(_T("\\settings\\SwordBible.ini"),  _T("\\settings") );
    CreateDirectory(dir, NULL);
  }
#else
	path = CSwordBibleDoc::driveLetter + _T(":\\SwordBible.ini");
#endif


  
  //First free the string allocated by MFC at CWinApp startup.
	//The string is allocated before InitInstance is called.
	free((void*)m_pszProfileName);
	//Change the name of the .INI file.
	//The CWinApp destructor will free the memory.
	m_pszProfileName=_tcsdup(path);

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	//SetRegistryKey(_T("Local AppWizard-Generated Applications"));
	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CSwordBibleDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CSwordBibleView));
	if (!pDocTemplate)
		return FALSE;
	AddDocTemplate(pDocTemplate);
	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	// Dispatch commands specified on the command line.  Will return FALSE if
	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
	if (!ProcessShellCommand(cmdInfo))
			return FALSE;



	// The one and only window has been initialized, so show and update it
	  m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
	  ((CMainFrame *) m_pMainWnd)->RestorePosition();
	   ((CMainFrame *) m_pMainWnd)->Normal();
	   m_pMainWnd->UpdateWindow();

	   // restore the state the toolbars
	CFrameWnd * pWnd = ((CMainFrame *) m_pMainWnd)->m_wndQuickVerseBar.GetDockingFrame();
	if(pWnd->GetSafeHwnd()) pWnd->LoadBarState(_T("QuickVerseDockState"));

	 CMainFrame * pMWnd = ((CMainFrame *) m_pMainWnd);
	 pMWnd->m_bibleDlg.LoadState(_T("Bibles"));
	 pMWnd->m_comDlg.LoadState(_T("Commentaries"));
	 pMWnd->m_dictDlg.LoadState(_T("Lexicons / Dictionaries"));
	 pMWnd->m_treeDlg.LoadState(_T("Navigation"));
	
	
	if (CSwordBibleDoc::devotion_startup == TRUE)
		(CFrameWnd*)m_pMainWnd->PostMessage(WM_SHOW_DEVOTIONAL, (WPARAM) NULL, (LPARAM) NULL);

    factory.Close();



	return TRUE;
}




// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
	CEdit agreement;
	CTransparentImage gnu;
	virtual BOOL OnInitDialog();
	afx_msg void OnStnClickedStaticMail();
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	afx_msg void OnStnClickedStaticWebsite();
	afx_msg void OnClickDonate();
	afx_msg void OnBnClickedOk();
public:
	afx_msg void OnPaint();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_AGREEMENT, agreement);
	DDX_Control(pDX, IDC_STATIC_GNU, gnu);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	ON_STN_CLICKED(IDC_STATIC_MAIL, OnStnClickedStaticMail)
	ON_WM_SETCURSOR()
	ON_WM_CTLCOLOR()
	ON_STN_CLICKED(IDC_STATIC_WEBSITE, OnStnClickedStaticWebsite)
	ON_STN_CLICKED(IDC_STATIC_DONATE, OnClickDonate)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_WM_PAINT()
END_MESSAGE_MAP()

// App command to run the dialog
void CSwordBibleApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}


// CSwordBibleApp message handlers


BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	agreement.SetWindowText(_T("SwordBible is free software; you can redistribute ")
    _T("it and/or modify it under the terms of the GNU General Public License ")
    _T("as published by the Free Software Foundation version 2 of the License.\r\n\r\n")

    _T("This software has been made possible by people at Crosswire Society. Here are a few: \r\n")
    _T("Barry Drake,  Bayu Gunawan, Bill Kincaid, Brook Humphrey, Chris Bitmead, Chris Little, Daniel Glassey,")
    _T("Darren DeMeulenaere, Darwin Gregory, David Haslam, David Trotz Jr., DM Smith, Eeli Kaikkonen,")
    _T("Franklin Bratcher, Geoffrey W. Hastings, Greg Hellings, Gregory Hall, Jason Turner, Jerry Hastings,")
    _T("Jerry Kingery, Joachim Ansorg, Joe Walker, Jonathan Morgan, Karl kleinpaste, Kristof Petr, Larry Pierce,")
    _T("Luis Cortes, Mark Fuller, Martin Gruner, Martin Gruner, Matthew Talbert, Michael Paul Johnson,")
    _T("Paul Gear, Peter von Kaehne, Roland Nygren, Steve Hiner, Troy A. Griffitts, Terry Biggs,")
    _T("Torsten Uhlmann, Will Thimbleby, William Dicks, Karl-Heinz Troyer, and many more.....\r\n\r\n")

	_T("This program is distributed in the hope that it will be useful, ")
    _T("but WITHOUT ANY WARRANTY; without even the implied warranty of ")
    _T("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n\r\n")
   _T("You should have received a copy of the GNU General Public License ")
    _T("along with this program.  If not, see http://www.gnu.org/licenses/"));

	

		
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CAboutDlg::OnStnClickedStaticMail()
{
	ShellExecute(NULL, _T("open"), _T("mailto:mital.manu@gmail.com?SUBJECT=SwordBible 5.4"),
		NULL, NULL, SW_SHOWNORMAL);
}

BOOL CAboutDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	if ( pWnd->GetDlgCtrlID() == IDC_STATIC_MAIL ||
		pWnd->GetDlgCtrlID() == IDC_STATIC_WEBSITE ||
		pWnd->GetDlgCtrlID() == IDC_STATIC_DONATE)
		{ SetCursor (CBibleSheet::m_hHandCursor); return TRUE; }

	else return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

		if (pWnd->GetDlgCtrlID() == IDC_STATIC_MAIL ||
			pWnd->GetDlgCtrlID() == IDC_STATIC_WEBSITE )		
			pDC->SetTextColor(0xff0000);


	// TODO:  Change any attributes of the DC here

	return hbr;
}

void CAboutDlg::OnStnClickedStaticWebsite()
{
	ShellExecute(NULL, _T("open"), _T("http://www.swordwarrior.net"),
		NULL, NULL, SW_SHOWNORMAL);

}

void CAboutDlg::OnClickDonate()
{
	// TODO: Add your control notification handler code here
CString donate = _T("https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business")
_T("=mmital%40vt%2eedu&item_name=Donate%20to%20SwordBible&no_shipping=0&no_note=1&tax")
_T("=0&currency_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8");

ShellExecute(NULL, _T("open"), donate,
		NULL, NULL, SW_SHOWNORMAL);

}
void CAboutDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}


void CAboutDlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages
	 // show the picture
 
}
